package com.tentact.servlets;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tentact.service.DeptService;
import com.tentact.vo.Department;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

import java.util.List;

@WebServlet(name = "SelectDeptnameServlet", value = "/SelectDeptnameServlet")
public class SelectDeptnameServlet extends HttpServlet {
    DeptService service =new DeptService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("进入ajax");
        List<Department> departments =service.selectAllDept();

        //将集合转换为json
        ObjectMapper mapper =new ObjectMapper();
        String proString =mapper.writeValueAsString(departments);
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out =response.getWriter();
        out.print(proString);
        out.flush();
        out.close();

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
